<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="内码" prop="fClientCode">
        <el-input v-model="queryParams.fClientCode" placeholder="请输入内码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="添加日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="岗位名称" prop="fClientCode">
        <el-input
          v-model="queryParams.fClientCode"
          placeholder="请输入岗位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="岗位状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增客户</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clientList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="fId" />
      <el-table-column label="客户" align="center" prop="fClientCode" />
      <!-- <el-table-column label="Lab值" align="center" prop="fLabNum" /> -->
      <el-table-column label="备注" align="center" prop="fRemark" />
      <el-table-column label="创建人" align="center" prop="fCreaterName" />
      <el-table-column label="创建日期" align="center" prop="fCreateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fCreateDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改人" align="center" prop="fModifierName" />
      <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fModifyDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" min-width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row,1)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户" prop="fClientCode">
          <el-input v-model="form.fClientCode" placeholder="请输入客户" :disabled="isEdit == 1" />
        </el-form-item>
        <!-- <el-form-item label="Lab值" prop="fLabNum">
          <el-input v-model="form.fLabNum" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item label="备注" prop="fRemark">
          <el-input v-model="form.fRemark" type="textarea" placeholder="请输入内容" :disabled="isEdit == 1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isEdit!=1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listClient, getClient, delClient, addClient, updateClient } from "@/api/basicData/customer";
export default {
  name: "Client",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      isEdit: 0,
      dateRange: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      clientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fClientCode: undefined
      },
      // 表单参数
      form: {
        fId: undefined,
        fClientCode: '',
        fRemark: ''
      },
      // 表单校验
      rules: {
        fClientCode: [
          { required: true, message: "客户不能为空", trigger: "blur" }
        ]
        
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addDateRange(params, dateRange) {
      params.fCreateDateStart = dateRange[0];
      params.fCreateDateEnd = dateRange[1];
      return params;
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listClient(this.addDateRange(this.queryParams, this.dateRange))
        .then(response => {
          this.clientList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(error => {
          console.error('获取客户列表失败', error);
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.resetForm();
    },
    // 表单重置
    resetForm() {
      this.form = {
        fId: undefined,
        fClientCode: '',
        fRemark: ''
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        fClientCode: undefined
      };
      this.dateRange = [];
      this.resetForm();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isEdit = 0
      this.resetForm();
      this.open = true;
      this.title = "新增客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.isEdit = type
      this.resetForm();
      const fId = row.fId || this.ids
      getClient(fId)
        .then(response => {
          this.form = response.data;
          this.open = true;
          if (type === 1) {
          this.open = true;
          this.title = "查看客户";
        } else {
          this.open = true;
          this.title = "修改客户";
        }      
        })
        .catch(error => {
          console.error('获取客户详情失败', error);
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fId) {
            updateClient(this.form)
              .then(response => {
                this.$modal.msgSuccess('修改成功');
                this.open = false;
                this.getList();
              })
              .catch(error => {
                console.error('修改客户失败', error);
              });
          } else {
            addClient(this.form)
              .then(response => {
                this.$modal.msgSuccess('新增成功');
                this.open = false;
                this.getList();
              })
              .catch(error => {
                console.error('新增客户失败', error);
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clientId = row.fId || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + clientId + '"的数据项？')
        .then(() => {
          delClient(clientId)
            .then(() => {
              this.getList();
              this.$modal.msgSuccess('删除成功');
            })
            .catch(error => {
              console.error('删除客户失败', error);
            });
        })
        .catch(() => { });
    }
  }
};
</script>